import { Component, OnInit } from '@angular/core';
import { DemoListService } from './list.service';
import { PaginationModel } from 'app/widgets/pagination';
import { environment } from '../../../../environments/environment';
import * as flatpickr from 'flatpickr';
import { zh } from "flatpickr/dist/l10n/zh.js";
import * as _ from 'lodash';
import * as moment from 'moment';

@Component({
	selector: 'demo-list',
	templateUrl: './list.component.html',
	styleUrls: ['./list.component.scss'],
	providers: [DemoListService]
})

export class DemoListComponent implements OnInit {
	// 分页列表post
	public getBody = {
		pageIndex: 1,
		pageSize: 10
	};
	// 分页配置
	public listPageInfo: PaginationModel = {
		currentPageNum: 1,
		pageSize: 10,
		totalPages: 1,
		total: 0,
		pagesShow: 5,
		startRow: 0,
		endRow: 0,
		pageList: [5, 10, 25, 50, 100]
	};
	// 分页列表数据
	public dataList: any;
	// 列表全选
	public isCheckedAll: any = false;
	// 字典值
	public dictionaryList: any = {
		S_SCHEDULE_JOB: []
	};
	// 新增/修改body
	public editBody: any = {};
	// 新增/修改操作
	public operate: any;

	constructor(
		private demoListService: DemoListService
	) { }

	ngOnInit() {
		// 字典值
		this.dictionaryList = JSON.parse(localStorage.getItem("dictionaryList"));
		this.list();
	}

	// 点击表格头排序
	orderData(colName) {
		let listDataAsc = _.orderBy(this.dataList, [colName], ["asc"]);
		if (JSON.stringify(this.dataList) == JSON.stringify(listDataAsc)) {
			listDataAsc = _.orderBy(this.dataList, [colName], ["desc"]);
		}
		this.dataList = listDataAsc;
	}
	// 分页列表
	list(pageNum?, pageSize?) {
		let that = this;
		this.isCheckedAll = false;
		this.getBody.pageIndex = pageNum ? pageNum : this.listPageInfo.currentPageNum;
		this.getBody.pageSize = pageSize ? pageSize : this.listPageInfo.pageSize;
		// this.getBody.sFundCode = _.trim(this.getBody.sFundCode);
		// this.getBody.sFundName = _.trim(this.getBody.sFundName);
		this.demoListService.list(this.getBody).subscribe(data => {
			if (data) {
				that.dataList = data.list;
				that.listPageInfo.currentPageNum = data.pageNum;
				that.listPageInfo.totalPages = data.pages;
				that.listPageInfo.total = data.total;
				that.listPageInfo.startRow = data.startRow;
				that.listPageInfo.endRow = data.endRow;
			}
		});
	}
	//个数分页
	listPageSizeChange(pageSize: number) {
		if (pageSize !== this.listPageInfo.pageSize) {
			this.listPageInfo.pageSize = pageSize;
			this.list();
		}
	}
	// 条数分页
	listPageNavigation(currentPageNum: number) {
		this.listPageInfo.currentPageNum = currentPageNum;
		this.list(this.listPageInfo.currentPageNum, this.listPageInfo.pageSize);
	}
	// 列表全选
	checkedAll() {
		if (this.isCheckedAll) {
			_.forEach(this.dataList, item => {
				item.isChecked = true;
			});
		} else {
			_.forEach(this.dataList, item => {
				item.isChecked = false;
			});
		}
	}
	// 列表是否全选
	ctrlCheckAll() {
		let that = this;
		let checkList = [];
		_.forEach(this.dataList, item => {
			if (item.isChecked == true) {
				checkList.push(item);
			}
		})
		if (checkList.length == this.dataList.length) {
			this.isCheckedAll = true;
		} else {
			this.isCheckedAll = false;
		}
	}
	// 新增/修改modal,operate为操作状态
	showModel(operate) {
		let that = this;
		let checkedItem = [];
		this.operate = operate;
		_.forEach(this.dataList, item => {
			if (item.isChecked) {
				checkedItem.push(_.cloneDeep(item));
			}
		});
		switch (operate) {
			case 'add':
				that.editBody = {};
				// 任务状态默认为0
				that.editBody.jobStatus = 0;
				window["$"]("#editModel").modal("show");
				break;
			case 'edit':
				if (checkedItem.length == 1) {
					that.editBody = checkedItem[0];
					window["$"]("#editModel").modal("show");
				} else {
					window['swal']("提示", "请选择一条数据进行操作", "warning");
				}
				break;
		}
	}
	// 新增/修改保存
	save() {
		let that = this;
		switch (this.operate) {
			case 'add':
				window['swal']({
					type: "warning",
					showCancelButton: true,
					confirmButtonText: "确认",
					cancelButtonText: "取消",
					closeOnConfirm: false,
					showLoaderOnConfirm: true,
					title: "提示",
					text: "确认新增吗",
					html: true
				}, function (isConfirm) {
					if (isConfirm) {

					}
				})
				break;
			case 'edit':
				window['swal']({
					type: "warning",
					showCancelButton: true,
					confirmButtonText: "确认",
					cancelButtonText: "取消",
					closeOnConfirm: false,
					showLoaderOnConfirm: true,
					title: "提示",
					text: "确认修改吗",
					html: true
				}, function (isConfirm) {
					if (isConfirm) {

					}
				})
				break;
		}
	}
	// 字典值获取
	getValue(){
		
	}
}
